import BaseController from "../../cfw/mvc/BaseController";
import ModuleManager, { ModuleID } from "../../cfw/module/ModuleManager";
import { UIIndex, ItemState } from "../../cfw/tools/Define";

import LobbyController from "../lobby/LobbyController";
import DressingRoomManager from "./model/DressingRoomManager";
import ColumnItemModel from "./model/ColumnItemModel";
import ClotheItemModel from "./model/ClotheItemModel";
import GlobalEvent from "../../cfw/event/GlobalEvent";
import { EventName, ItemID, GameText } from "../../public/Config";
import DecorationControllerInterface from "../../public/interface/DecorationControllerInterface";
import ItemController from "../item/ItemController";
import BagManager from "../bag/BagManager";
import { ResultState, SDKEventName } from "../../sdk/SDKConfig";
import TipController from "../tip/TipController";
import SDKManager from "../../sdk/SDKManager";

export default class DressingRoomController extends BaseController implements DecorationControllerInterface, ItemController {


    private static ins: DressingRoomController;

    static instance() {
        if (!this.ins) {
            this.ins = new DressingRoomController()
        }
        return this.ins;
    }

    back() {
        // this.intoSuitcases()
        this.intoLobby();
    }

    // intoSuitcases() {
    //     ModuleManager.setModuleID(ModuleID.DECORATION)
    //     this.pushView('prefabs/SuitcasesView', 'SuitcasesView', null, ModuleManager.getLoader(), UIIndex.ROOT)
    // }



    intoDressingRoom(suitID: number) {
        ModuleManager.setModuleID(ModuleID.DECORATION)
        this.pushView('prefabs/DressingRoomView', 'DressingRoomView', new DressingRoomManager(suitID), ModuleManager.getLoader(), UIIndex.ROOT)
    }

    intoLobby() {
        LobbyController.instance().intoLayer()
    }

    protected tempColumn: ColumnItemModel;
    selectColumn(model: ColumnItemModel) {
        if (this.tempColumn != model) {
            this.tempColumn = model;
            GlobalEvent.instance().emit(EventName.SELECT_COLUMN, model.getID())
        }

    }
    protected tempClothe: ClotheItemModel;
    onButtonIconClick(model: ClotheItemModel) {
        if (!model.isOpen()) {
            TipController.instance().showToast(GameText.ITEM_TOAST, { num: model.getOpenLevel() })
            return;
        }
        if (model.needBuy()) {
            let cost: number[] = model.getCostItem();
            if (BagManager.isEnough(cost[0], cost[1])) {
                BagManager.costItem(cost[0], cost[1], (s) => {
                    if (s == ResultState.YES) {
                        model.setState(ItemState.GOT)
                    }
                })
            }
        } else {
            this.useItem(model)
        }
       

    }

    useItem(model) {
        if (this.tempClothe != model) {
            this.tempClothe = model;
            model.use()
            SDKManager.getChannel().sendEvent(SDKEventName.dress,model.getID())
            // GlobalEvent.instance().emit(EventName.SELECT_CLOTHE, model)
        } else {
            if (this.tempClothe.canDel()) {
                this.tempClothe.unUse();
                this.tempClothe = null;
            }


        }
    }


}
